﻿@{
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form layui-form-pane" action="" lay-filter="editForm">
    <input type="hidden" name="id" />

    <div class="layui-form-item">
        <label class="layui-form-label">委托人</label>
        <div class="layui-input-block">
            <input type="text" name="consignor" lay-verify="required" placeholder="请输入仓库编码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">商品编码</label>
        <div class="layui-input-block">
            <input type="text" name="commodityCode" lay-verify="required" placeholder="请输入仓库名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
            <input type="text" name="productName" placeholder="请输入位置" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单位</label>
        <div class="layui-input-block">
            <select id="unit" name="unit">
                <option value="">请选择</option>
            </select>

        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">产品类别</label>
        <div class="layui-input-block">
            <input type="text" name="productCategory" placeholder="请输入负责人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单层</label>
        <div class="layui-input-block">
            <input type="text" name="singleLayer" placeholder="请输入负责人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">产品属性</label>
        <div class="layui-input-block">
            <input type="text" name="productAttribute" placeholder="请输入负责人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">基础单位</label>
        <div class="layui-input-block">
            <select id="basicUnit" name="basicUnit">
                <option value="">请选择</option>
                <option value="1">厘米</option>
                <option value="2">米</option>
            </select>

        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">客户商品编码</label>
        <div class="layui-input-block">
            <input type="text" name="customerCommodityCode" placeholder="请输入负责人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">破损情况</label>
        <div class="layui-input-block">
            <input type="text" name="breaking" placeholder="请输入负责人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>

<script>
    layui.use(['form'], function(){
        var form = layui.form;
        var $ = layui.$;
        var layer = layui.layer;

        // 加载数据方法
        window.loadData = function(id) {
            $.get('/CommodityDetail/GetCommodityDetailById', { id: id }, function(res){
                console.log('获取到的数据:', res);
                if(res.code === "0"){
                    var formData = {
                        id: res.data.result.id,
                        consignor: res.data.result.consignor,
                        commodityCode: res.data.result.commodityCode,
                        productName: res.data.result.productName,
                        unit: res.data.result.unit,
                        productCategory: res.data.result.productCategory,
                        singleLayer: res.data.result.singleLayer,
                        productAttribute: res.data.result.productAttribute,
                        basicUnit: res.data.result.basicUnit,
                        customerCommodityCode: res.data.result.customerCommodityCode,
                        breaking: res.data.result.breaking,

                    };
                    form.val('editForm', formData);
                    form.render();
                } else {
                    layer.msg('获取数据失败');
                }
            });
        }

        // 监听提交
        form.on('submit(saveBtn)', function(data){
            $.ajax({
                url: '/CommodityDetail/UpdateCommodityDetail',
                type: 'POST',
                data: data.field,
                success: function(res){
                    console.log('保存结果:', res);
                    if(res > 0){
                        alert('保存成功');
                        // 刷新父页面表格
                        parent.layui.table.reload('Table');
                        // 关闭弹窗
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }else{
                        alert('保存失败');
                    }
                }
            });
            return false;
        });

         var unit = function(){
                $.ajax({
                    url: "/CommodityDetail/GetDictItemById",
                    type: "get",
                    dataType: "json",
                    data: { id: 1 },
                    success: function (res) {
                        console.log(res)
                        var data = res;
                        var html = "";
                        for (var i = 0; i < data.length; i++) {
                            html += "<option value='" + data[i].sortId + "'>" + data[i].dictItemName + "</option>";
                        }
                        $("#unit").html(html);
                        form.render('select');
                    }
                });
           }

           unit();


    });
</script> 